package com.daxt.controller.warehouse;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.daxt.base.common.util.RedisUtil;
import com.daxt.base.common.util.UserInfoUtil;
import com.daxt.mapper.sys.ArchivesFondsMapper;
import com.daxt.mapper.warehouse.WarehouseShelfPostionFileMapper;
import com.daxt.mapper.warehouse.WarehouseShelfPostionMapper;
import com.daxt.model.service.archives.vo.ArchivesFonds;
import com.daxt.model.service.base.result.UserInfo;
import com.daxt.model.service.warehouse.vo.WarehouseShelfPostion;
import com.daxt.model.service.warehouse.vo.WarehouseShelfPostionFile;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.daxt.common.result.Result;
import com.daxt.common.result.ResultCode;
import com.daxt.common.result.ResultUtil;
import com.daxt.mapper.warehouse.WarehouseMapper;
import com.daxt.mapper.warehouse.WarehouseShelfMapper;
import com.daxt.model.service.warehouse.param.SortWarehouse;
import com.daxt.model.service.warehouse.vo.Warehouse;
import com.daxt.model.service.warehouse.vo.WarehouseShelf;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpServletRequest;

@Api(tags = "库房")
@RestController
@RequestMapping("/warehouse")
public class WarehouseController {

	@Autowired
	private WarehouseMapper warehouseMapper;
	@Autowired
	private WarehouseShelfMapper warehouseShelfMapper;
	@Autowired
	private WarehouseShelfPostionMapper warehouseShelfPostionMapper;
	@Autowired
	private WarehouseShelfPostionFileMapper WarehouseShelfPostionFileMapper;
	@Autowired
	private UserInfoUtil userInfoUtil;
	@Autowired
	private RedisUtil redisUtil;
	@Autowired
	private ArchivesFondsMapper ArchivesFondsMapper;

	@ApiOperation(value = "库房列表", notes = "") 
	@GetMapping("/list") 
	public Result<List<Warehouse>> list(HttpServletRequest request){
		String fondsId=null;
		QueryWrapper<Warehouse> warehouseQueryWrapper=new QueryWrapper<>();
		UserInfo loginInfo = userInfoUtil.getLoginInfo(UserInfo.class, redisUtil, request);
		if (loginInfo == null) {
			return ResultUtil.data(ResultCode.NOT_LOGIN.getCode(), ResultCode.NOT_LOGIN.getMes(), null);
		}
		if("admin".equals(loginInfo.getUsername()) || "超级管理员".equals(loginInfo.getRole())){

		}else {
			fondsId=loginInfo.getFondsId();
		}
		warehouseQueryWrapper.eq(StringUtils.isNotEmpty(fondsId),"fondsId",fondsId);
		List<Warehouse> list = warehouseMapper.selectList(warehouseQueryWrapper);
		for(int i=0;i<list.size();i++){
			String fondId=list.get(i).getFondsId();
			if(StringUtils.isNotEmpty(fondId)){
				System.out.println(fondId);
				ArchivesFonds archivesFonds = ArchivesFondsMapper.selectById(fondId);
				if (archivesFonds!=null){
					list.get(i).setFondsName(archivesFonds.getFondsName());
				}

			}
		}
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, list);
	}
	
	@ApiOperation(value = "库房添加", notes = "") 
	@PostMapping("") 
	public Result<String> save(@RequestBody Warehouse entity){
		warehouseMapper.insert(entity);
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, null);
	}
	
	@ApiOperation(value = "库房修改", notes = "") 
	@PutMapping("")
	@Transactional
	public Result<List<Warehouse>> update(@RequestBody Warehouse entity){
		warehouseMapper.updateById(entity);
		autoWarehouse(entity.getId(),entity.getRowNum(),entity.getColumnNum());
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, null);
	}
	
	@ApiOperation(value = "排序", notes = "") 
	@PutMapping("/sort") 
	@Transactional(rollbackFor = Exception.class)
	public Result<String> sort(@RequestBody List<SortWarehouse> entity){
		try {
		if(entity!=null&&entity.size()>0) {
			for (SortWarehouse sortWarehouse : entity) {
				Warehouse warehouse=new Warehouse();
				warehouse.setId(sortWarehouse.getId());
				warehouse.setSort(sortWarehouse.getSort());
				warehouseMapper.updateById(warehouse);
			}
			return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, null);
		}else {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), null, null);
		}
		}catch (Exception e) {
			e.printStackTrace();
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "操作异常！！！", null);
		}
	}
	
	@ApiOperation(value = "库房删除", notes = "") 
	@DeleteMapping("") 
	@Transactional(rollbackFor = Exception.class)
	public Result<List<String>> del(@RequestBody List<String> ids){
		if(ids!=null&&ids.size()>0) {
			try {
			List<String> cIds=new ArrayList<String>();
			for (String string : ids) {
				QueryWrapper<WarehouseShelf> queryWrapper=new QueryWrapper<WarehouseShelf>();
				queryWrapper.eq("wId", string);
				List<WarehouseShelf> list = warehouseShelfMapper.selectList(queryWrapper);
				if(list!=null&&list.size()>0) {
					cIds.add(string);
				}
			}
			if(cIds.size()>0) {
				return ResultUtil.data(ResultCode.RESULT_ERROR_IDS.getCode(), "", cIds);
			}
			warehouseMapper.deleteBatchIds(ids);
			return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), "", null);
			}catch (Exception e) {
				e.printStackTrace();
				return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "操作异常！！！", null);
			}
			}else {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "删除至少选择一个库房！！！", null);
		}
	}


	private void autoWarehouse(long warehouseId,int row,int col){
		//获取对应库房下的所有柜架
		QueryWrapper<WarehouseShelf> warehouseShelfWrapper=new QueryWrapper<>();
		warehouseShelfWrapper.eq("wId",warehouseId);
		List<WarehouseShelf> warehouseShelfList=warehouseShelfMapper.selectList(warehouseShelfWrapper);
		Map<String,String> map =new HashMap<>();
		for (WarehouseShelf warehouseShelf:warehouseShelfList){
			QueryWrapper<WarehouseShelfPostion> warehouseShelfPostionfWrapper=new QueryWrapper<>();
			warehouseShelfPostionfWrapper.eq("sId",warehouseShelf.getId());
			List<WarehouseShelfPostion>  warehouseShelfPostionList=warehouseShelfPostionMapper.selectList(warehouseShelfPostionfWrapper);
			for(WarehouseShelfPostion warehouseShelfPostion:warehouseShelfPostionList){
				long sId = warehouseShelfPostion.getSId();
				int rowIndex = warehouseShelfPostion.getRowIndex();
				int colIndex = warehouseShelfPostion.getColIndex();
				String key=sId+","+rowIndex+","+colIndex;
				map.put(key,"1");
			}
			//最后对每个柜架，自动生成行与列
			for(int i=1;i<=row;i++){
				for(int j=1;j<=col;j++){
					String key=warehouseShelf.getId()+","+i+","+j;
					String value = map.get(key);
					if("1".equals(value)){
						continue;
					}
					WarehouseShelfPostion shelfPostion=new WarehouseShelfPostion();
					shelfPostion.setSId(warehouseShelf.getId());
					shelfPostion.setPname(i+"行"+j+"列");
					shelfPostion.setRowIndex(i);
					shelfPostion.setColIndex(j);
					warehouseShelfPostionMapper.insert(shelfPostion);
				}
			}
			//删除大于设置行数与列数的位置
			QueryWrapper<WarehouseShelfPostion> shelfPostionfWrapper=new QueryWrapper<>();
			shelfPostionfWrapper.gt("rowIndex",row);
			shelfPostionfWrapper.eq("sId",warehouseShelf.getId());
			List<WarehouseShelfPostion>  rowShelfPostionList=warehouseShelfPostionMapper.selectList(shelfPostionfWrapper);
			List<Long> ids=new ArrayList<>();
			for (WarehouseShelfPostion WarehouseShelfPostion:rowShelfPostionList){
				long id= WarehouseShelfPostion.getId();
				ids.add(id);
			}
			shelfPostionfWrapper=new QueryWrapper<>();
			shelfPostionfWrapper.gt("colIndex",col);
			shelfPostionfWrapper.eq("sId",warehouseShelf.getId());
			rowShelfPostionList=warehouseShelfPostionMapper.selectList(shelfPostionfWrapper);
			for (WarehouseShelfPostion WarehouseShelfPostion:rowShelfPostionList){
				long id= WarehouseShelfPostion.getId();
				ids.add(id);
			}
			if(ids.size()>0){
				warehouseShelfPostionMapper.deleteBatchIds(ids);
				QueryWrapper<WarehouseShelfPostionFile> warehouseShelfPostionFileQueryWrapper=new QueryWrapper<>();
				warehouseShelfPostionFileQueryWrapper.in("pId",ids);
				WarehouseShelfPostionFileMapper.delete(warehouseShelfPostionFileQueryWrapper);
			}
		}
	}
	
}
